package com.noahplat.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.user.mapper.*;
import com.noahplat.user.pojo.*;
import com.noahplat.user.service.RoleService;
import com.noahplat.user.utils.RoleDao;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * 角色实现类
 * 宋彦磊
 * 2021.03.25
 */

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    /**
     * 添加角色
     */
    @Override
    public Result addRole(Role role) {
        Example example = new Example(User.class);
        example.createCriteria().orEqualTo("id", role.getUid());
        User user = userMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(user)) {
            return new Result(false, StatusCode.ERROR, "添加角色失败");
        }
        role.setCreatedBy(user.getName());
        role.setCreatedTime(new Date());
        int res = roleMapper.insert(role);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "添加角色失败");
        }
        return new Result(true, StatusCode.OK, "添加角色成功");
    }

    /**
     * 获取角色列表
     */
    @Override
    public Result getRole(Integer page, Integer limit, Long acctid, Long stid, String description) {
        PageHelper.startPage(page, limit);
        Example example = new Example(Role.class);
        example.createCriteria().andEqualTo("acctid", acctid).andEqualTo("stid", stid);
        if (!StringUtils.isEmpty(description)) {
            example.and(example.createCriteria().andLike("description", description + "%"));
        }
        List<Role> list = roleMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "无角色信息");
        }
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 获取角色列表
     */
    @Override
    public Result selectRole(Long acctid, Long stid) {
        Example example = new Example(Role.class);
        example.createCriteria().andEqualTo("acctid", acctid).andEqualTo("stid", stid);
        List<Role> list = roleMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "无角色信息");
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 修改角色信息
     */
    @Override
    public Result updateRole(Role role) {
        int res = roleMapper.updateByPrimaryKey(role);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "修改角色失败");
        }
        return new Result(true, StatusCode.OK, "修改角色成功");
    }

    /**
     * 根绝ID获取角色信息
     */
    @Override
    public Result getRoleById(Long id) {
        Example example = new Example(Role.class);
        example.createCriteria().andEqualTo("id", id);
        Role role = roleMapper.selectOneByExample(example);
        if (ObjectUtils.isEmpty(role)) {
            return new Result(false, StatusCode.NOTFOUNDERROR, "无此角色信息");
        }

        Example example1 = new Example(Menu.class);
        example1.createCriteria().andEqualTo("stid", role.getStid()).andEqualTo("acctid", role.getAcctid()).andEqualTo("del", "N");
        List<Menu> list = menuMapper.selectByExample(example1);
        if (ObjectUtils.isEmpty(list)) {
            return new Result(false, StatusCode.ERROR, "无菜单信息");
        }
        Example example2 = new Example(RoleMenu.class);
        example2.createCriteria().andEqualTo("rid", id);
        List<RoleMenu> list1 = roleMenuMapper.selectByExample(example2);
        if (!ObjectUtils.isEmpty(list1)) {
            for (RoleMenu roleMenu : list1) {
                for (Menu menu : list) {
                    if (roleMenu.getMid() == menu.getId()) {
                        menu.setState("1");
                        break;
                    }
                }
            }
        }

        Example example3 = new Example(Permission.class);
        example3.createCriteria().andEqualTo("stid", role.getStid()).andEqualTo("acctid", role.getAcctid());
        List<Permission> list2 = permissionMapper.selectByExample(example3);
        if (ObjectUtils.isEmpty(list2)) {
            return new Result(false, StatusCode.ERROR, "无接口权限信息");
        }
        Example example4 = new Example(RolePermission.class);
        example4.createCriteria().andEqualTo("rid", role.getId());
        List<RolePermission> list3 = rolePermissionMapper.selectByExample(example4);
        if (!ObjectUtils.isEmpty(list3)) {
            for (RolePermission rolePermission : list3) {
                for (Permission permission : list2) {
                    if (rolePermission.getPid() == permission.getId()) {
                        permission.setState("1");
                        break;
                    }
                }
            }
        }

        List<Role> list4 = new ArrayList<>();
        list4.add(role);
        Map<String, List> map = new HashMap<>();
        map.put("role", list4);
        map.put("menu", list);
        map.put("permission", list2);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }

    /**
     * 根据ID删除角色
     */
    @GlobalTransactional
    @Override
    public Result delRole(Long[] id) {
        List<Long> list = new ArrayList<>(Arrays.asList(id));
        Example example = new Example(Role.class);
        example.createCriteria().andIn("id", list);
        int res = roleMapper.deleteByExample(example);
        if (res == 0) {
            return new Result(false, StatusCode.ERROR, "删除失败");
        }

        Example example1 = new Example(RoleMenu.class);
        example1.createCriteria().andIn("rid", list);
        List<RoleMenu> list1 = roleMenuMapper.selectByExample(example1);
        if (!ObjectUtils.isEmpty(list1)) {
            int delres = roleMenuMapper.deleteByExample(example1);
            if (delres == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器报错");
            }
        }

        Example example2 = new Example(RolePermission.class);
        example2.createCriteria().andIn("rid", list);
        List<RolePermission> list2 = rolePermissionMapper.selectByExample(example2);
        if (!ObjectUtils.isEmpty(list2)) {
            int delres = rolePermissionMapper.deleteByExample(example2);
            if (delres == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器报错");
            }
        }

        Example example3 = new Example(UserRole.class);
        example3.createCriteria().andIn("rid", list);
        List<UserRole> list3 = userRoleMapper.selectByExample(example3);
        if (!ObjectUtils.isEmpty(list3)) {
            int delres = userRoleMapper.deleteByExample(example3);
            if (delres == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器报错");
            }
        }

        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 关联权限修改
     *
     * @return
     */
    public Result updateRoles(RoleDao roleDao) {
        Set<Long> setoldmid = new HashSet<Long>(Arrays.asList(roleDao.getOldmid()));
        Set<Long> nnewmid = new HashSet<Long>(Arrays.asList(roleDao.getNewmid()));
        setoldmid.removeAll(nnewmid);
        if (!ObjectUtils.isEmpty(setoldmid)) {
            Example example = new Example(RoleMenu.class);
            example.createCriteria().andEqualTo("rid", roleDao.getRid()).andEqualTo("acctid", roleDao.getAcctid()).andIn("mid", setoldmid);
            int res = roleMenuMapper.deleteByExample(example);
            if (res == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器出错");
            }
        }
        Set<Long> setmold = new HashSet<Long>(Arrays.asList(roleDao.getOldmid()));
        Set<Long> setnewmid = new HashSet<Long>(Arrays.asList(roleDao.getNewmid()));
        setnewmid.removeAll(setmold);
        if (!ObjectUtils.isEmpty(setnewmid)) {
            int res = roleMenuMapper.insertRoleMenu(roleDao.getAcctid(), roleDao.getRid(), setnewmid);
            if (res == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器出错");
            }
        }

        Set<Long> setoldpid = new HashSet<Long>(Arrays.asList(roleDao.getOldpid()));
        Set<Long> nnewpid = new HashSet<Long>(Arrays.asList(roleDao.getNewpid()));
        setoldpid.removeAll(nnewpid);
        if (!ObjectUtils.isEmpty(setoldpid)) {
            Example example = new Example(RolePermission.class);
            example.createCriteria().andEqualTo("acctid", roleDao.getAcctid()).andEqualTo("rid", roleDao.getRid()).andIn("pid", setoldpid);
            int res = rolePermissionMapper.deleteByExample(example);
            if (res == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器出错");
            }
        }
        Set<Long> setpold = new HashSet<Long>(Arrays.asList(roleDao.getOldpid()));
        Set<Long> setnewpid = new HashSet<Long>(Arrays.asList(roleDao.getNewpid()));
        setnewpid.removeAll(setpold);
        if (!ObjectUtils.isEmpty(setnewpid)) {
            int res = rolePermissionMapper.insertRolePermission(roleDao.getAcctid(), roleDao.getRid(), setnewpid);
            if (res == 0) {
                return new Result(false, StatusCode.SERVER_EXCEPTION, "服务器出错");
            }
        }
        return new Result(true, StatusCode.OK, "修改关联关系成功");
    }


}
